package com.hfb.mashibing.alip8.designpattern._09observer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import lombok.Data;

/**
 * 题目：模拟 宝宝在睡觉，醒来后哭了，之后要喂奶
 * 面向对象的傻等，直到醒来
 *
 * 很多时候，观察者要根据事件的具体情况来进行处理
 * 大多苏时候，我们处理事件的时候，需要事件源对象
 */

public class V7 {

    public static void main(String[] args) throws InterruptedException {
        ChildV7 baby = new ChildV7();
        TimeUnit.SECONDS.sleep(3);
        // do something
        baby.wakeUp();

    }
}

/**
 * 加入多个观察者
 */
class ChildV7 {
    private boolean cry = false;
    /*观察者与被观察者分离*/
    private List<ObserverV7> observers = new ArrayList<ObserverV7>(){{
        add(new MamV7());
        add(new DadV7());
        add(new NaiNaiV7());
    }};
    public ChildV7() {
        System.out.println("baby is doing sleepping !");
    }

    public boolean isCry() { return cry;}

    public void wakeUp(){
        cry = true;
        System.out.println("waked up! Crying! 555555555555555555");
        WakeUpEventV7 event = new WakeUpEventV7(
                                    System.currentTimeMillis(),
                                    "bad",
                                    this);
        observers.forEach(
            o -> o.actionOnWakeUp(event)
        );
    }
}

/**
 * 事件类
 */
@Data
class WakeUpEventV7{
    long timstamp;
    String loc;
    ChildV7 source;

    public WakeUpEventV7(long timstamp, String location, ChildV7 source) {
        this.timstamp = timstamp;
        this.loc = loc;
        this.source = source;
    }
}

/**
 * 加入观察者
 */
interface ObserverV7 {
    void actionOnWakeUp(WakeUpEventV7 t);
}

class DadV7 implements ObserverV7 {
    @Override
    public void actionOnWakeUp(WakeUpEventV7 t) {
        System.out.println("he is Crying! dadV7 is go to say !");
    }
}

class MamV7 implements ObserverV7 {
    @Override
    public void actionOnWakeUp(WakeUpEventV7 t) {
        System.out.println("he is Crying! MamV7 is go to hug !");
    }
}

class NaiNaiV7 implements ObserverV7 {
    @Override
    public void actionOnWakeUp(WakeUpEventV7 t) {
        System.out.println("he is Crying! NaiNaiV7 is go to feed !");
    }
}
